﻿using MediatR;
using RedDragon.Common.Domain.Manufacture;
using RedDragon.Common.Domain.Process;
using RedDragon.Common.Infrastructure.Interface;
using System.Diagnostics;
using T.Common.Domain.Common;
using Process = RedDragon.Common.Domain.Process.Process;

namespace RedDragon.RBAC.API.Application.Common.ProcessW
{
    public class ProcessDetailSearchHandler : IRequestHandler<ProcessDetailSearchCommon, Result>
    {
        public readonly IBaseRepository<Process> ProcessRepository;
        public readonly IBaseRepository<ProcessDetail> ProcessDetailRepository;
        public readonly IBaseRepository<Resources> ResourcesRepository;

        public ProcessDetailSearchHandler(IBaseRepository<Process> processRepository, IBaseRepository<ProcessDetail> processDetailRepository, IBaseRepository<Resources> resourcesRepository)
        {
            ProcessRepository = processRepository;
            ProcessDetailRepository = processDetailRepository;
            ResourcesRepository = resourcesRepository;
        }

        public async Task<Result> Handle(ProcessDetailSearchCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "" };
            try
            {
                if (request.Number != null)
                {
                    var id = ProcessRepository.Query().Where(x => x.ProcessNumber == request.Number).FirstOrDefault().ProcessId;
                    var list = from a in ProcessDetailRepository.Query()
                               join
                               b in ResourcesRepository.Query()
                                on a.ProcessDetailName equals b.ResourceCode
                               where a.ProcessId == id
                               select new ListDto()
                               {
                                   ProcessDetailId = a.ProcessDetailId,
                                   ProcessId = a.ProcessId,
                                   ProcessDetailName = a.ProcessDetailName,
                                   ResourceName = b.ResourceName,
                                   ProcessDetailUnitsName = a.ProcessDetailUnits == 1 ? "个/台" : a.ProcessDetailUnits == 2 ? "次" : a.ProcessDetailUnits == 3 ? "克" : a.ProcessDetailUnits == 4 ? "千克" : "小时",
                                   ProcessDetailDosage = a.ProcessDetailDosage,
                                   Summary = a.Summary,
                                   UnitsPrice = a.UnitsPrice,
                                   CreateTime = a.CreateTime,
                                   ProcessDetailStateName = a.ProcessDetailState == true ? "有效" : "无效",
                               };
                    result.Data = list;
                }
                else
                {
                    var list = from a in ProcessDetailRepository.Query()
                               join
                               b in ResourcesRepository.Query()
                                on a.ProcessDetailName equals b.ResourceCode
                               select new ListDto()
                               {
                                   ProcessDetailId = a.ProcessDetailId,
                                   ProcessId = a.ProcessId,
                                   ProcessDetailName = a.ProcessDetailName,
                                   ResourceName = b.ResourceName,
                                   ProcessDetailUnitsName = a.ProcessDetailUnits == 1 ? "个/台" : a.ProcessDetailUnits == 2 ? "次" : a.ProcessDetailUnits == 3 ? "克" : a.ProcessDetailUnits == 4 ? "千克" : "小时",
                                   ProcessDetailDosage = a.ProcessDetailDosage,
                                   Summary = a.Summary,
                                   UnitsPrice = a.UnitsPrice,
                                   CreateTime = a.CreateTime,
                                   ProcessDetailStateName = a.ProcessDetailState == true ? "有效" : "无效",
                               };
                    result.Data = list;
                }
               
               
              
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
    public class ListDto()
    {
        public int ProcessDetailId { get; set; }
        /// <summary>
        /// 工序管理外键
        /// </summary>
        public int ProcessId { get; set; }
        /// <summary>
        /// 资源名称
        /// </summary>
        public string ProcessDetailName { get; set; }
        /// <summary>
        /// 资源单位
        /// </summary>
        public int ProcessDetailUnits { get; set; }
        public string ProcessDetailUnitsName { get; set; }
        /// <summary>
        /// 资源用量
        /// </summary>
        public string? ProcessDetailDosage { get; set; }
        /// <summary>
        /// 摘要
        /// </summary>
        public string? Summary { get; set; }
        /// <summary>
        /// 成本
        /// </summary>
        public string? UnitsPrice { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 创建人
        /// </summary>
        public string? CreateName { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public bool ProcessDetailState { get; set; }
        public string ProcessDetailStateName { get; set; }
        public string ResourceName { get; set; }
    }
}
